"""
@Author  : 康帅
@Time    : 2021/3/19 15:38
@Function: 外观模式：解压zip、rar、7z案列
"""

from os import path
# 引入os处理路径

import logging
# 引入logging，进行错误日志记录


class ZipMode:
    """ zip """

    def compress(self, src_file_path, dst_file_path):
        print(f"zip模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")

    def decompress(self, src_file_path, dst_file_path):
        print(f"zip模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")


class RarMode:
    """ Rar """

    def compress(self, src_file_path, dst_file_path):
        print(f"rar模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")

    def decompress(self, src_file_path, dst_file_path):
        print(f"rar模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")


class ZMode:
    """ 7z """

    def compress(self, src_file_path, dst_file_path):
        print(f"7z模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")

    def decompress(self, src_file_path, dst_file_path):
        print(f"7z模块正在进行{src_file_path}文件压缩......")
        print(f"文件解压成功，以保存至{dst_file_path}")


class CompressionFacade:
    """ 压缩系统外观类 """

    def __init__(self):
        self.__zip_mode = ZipMode()
        self.__rar_mode = RarMode()
        self.__z_mode = ZMode()

    def compress(self, src_file_path, dst_file_path, types):
        """ 根据文件的类型不同压缩成不同的数据格式 """

        ext_name = "." + types  # 获取新的文件名
        full_name = dst_file_path + ext_name
        if types.lower() == "zip":
            self.__zip_mode.compress(src_file_path, full_name)
        elif types.lower() == "rar":
            self.__rar_mode.compress(src_file_path, full_name)
        elif types.lower() == "7z":
            self.__z_mode.compress(src_file_path, full_name)
        else:
            logging.error("Not support this format:" + str(types))
            return False
        return True

    def decompress(self, src_file_path, dst_file_path):
        """ 从src_file_path中获取后缀，根据不同的后缀名（扩展名），进行不同格式的数据压缩 """

        # path.basename 方法返回路径的最后一部分
        base_name = path.basename(src_file_path)
        ext_name = base_name.split(".")[1]
        if ext_name.lower() == "zip":
            self.__zip_mode.decompress(src_file_path, dst_file_path)
        elif ext_name.lower() == "rar":
            self.__rar_mode.decompress(src_file_path, dst_file_path)
        elif ext_name.lower() == "7z":
            self.__z_mode.decompress(src_file_path, dst_file_path)
        else:
            logging.error("Not support this format:" + str(ext_name))
            return False
        return True


if __name__ == '__main__':
    facade = CompressionFacade()
    facade.compress("E:\标准文件\生活中的外观模式.md", "E:\压缩文件\生活中的外观模式", "zip")
    facade.decompress("E:\压缩文件\生活中的外观模式.zip", "E:\标准文件\生活中的外观模式.md")
    print("*" * 60)

    facade.compress("E:\标准文件\python之美.pdf", "E:\压缩文件\python之美", "rar")
    facade.decompress("E:\压缩文件\python之美.rar", "E:\标准文件\python之美.pdf")
    print("*" * 60)

    facade.compress("E:\标准文件\项目重构.doc", "E:\压缩文件\python之美", "7z")
    facade.decompress("E:\压缩文件\项目重构.7z", "E:\标准文件\项目重构.doc")
